package com.gientech.service.impl;

import com.gientech.entity.LoanInfo;
import com.gientech.mapper.LoanInfoMapper;
import com.gientech.service.LoanInfoService;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.flowable.task.api.TaskQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;

@Service
public class LoanInfoServiceImpl implements LoanInfoService {

    @Autowired
    private LoanInfoMapper loanInfoMapper;
    @Autowired
    private ProcessEngine processEngine;

    @Override
    public void checkApply(LoanInfo loanInfo) {
        // 完成当前登录人任务
        TaskService taskService = processEngine.getTaskService();
        TaskQuery taskQuery = taskService.createTaskQuery().processDefinitionKey("loan");
        List<Task> taskList = taskQuery.taskAssignee("R01").processInstanceId(loanInfo.getTaskId()).list();

        HashMap<String, Object> map = new HashMap<>();
        map.put("approve", loanInfo.getApprove());
        for(Task task: taskList){
            taskService.complete(task.getId(), map);
        }

        // 更新贷款审批流程信息, 3:贷款发放
        loanInfo.setLoanStatus("3");
        loanInfoMapper.updateByPrimaryKeySelective(loanInfo);
    }

}
